package vo
{
	import flash.events.EventDispatcher;

	import iris.controls.StepController;

	import mx.collections.ArrayCollection;

	import nz.co.codec.flexorm.EntityManager;
	import nz.co.codec.flexorm.criteria.Criteria;

	import orm.fantasy.RBossItemVO;
	import orm.fantasy.RItemTokenVO;
	import orm.fantasy.RVendorSellVO;
	import orm.fantasy.TblBossVO;
	import orm.fantasy.TblItemPropertyVO;
	import orm.fantasy.TblItemVO;
	import orm.fantasy.TblVendorVO;

	public class ArmoryBean
	{
		private var _em:EntityManager;

		private var _itemVo:TblItemVO;
		private var _tokenItemVo:TblItemVO;
		private var _itemPropertyVo:TblItemPropertyVO;
		private var _itemDescription:ItemDescription;

		public static const EVENT_LOADING_COMPLETED:String = "beanEventLoadingCompleted";

		private var _stepController:StepController;

		public function ArmoryBean()
		{
			_em = EntityManager.instance;

			_stepController = new StepController(
				[loadTokenItem, loadDropInfo, loadVendorInfo, loadItemProperty]);
		}

		public function set itemVo(data:TblItemVO):void
		{
			_itemVo = data;

			_stepController.gotoNextStep();
		}

		public function get itemVo():TblItemVO
		{
			return _itemVo;
		}

		public function get itemPropertyVo():TblItemPropertyVO
		{
			return _itemPropertyVo;
		}

		public function get itemDescription():ItemDescription
		{
			return _itemDescription;
		}

		private function loadTokenItem():void
		{
			var tokenCriteria:Criteria = _em.createCriteria(RItemTokenVO);
			tokenCriteria.addEqualsCondition("tbl_item_id_token", _itemVo.id);
			var r:RItemTokenVO = _em.fetchCriteriaFirstResult(tokenCriteria) as RItemTokenVO;
			if(r != null)
			{
				var itemCriteria:Criteria = _em.createCriteria(TblItemVO);
				itemCriteria.addEqualsCondition("id", r.tbl_item_id);
				_tokenItemVo = _em.fetchCriteriaFirstResult(itemCriteria) as TblItemVO;
			}

			_itemDescription = new ItemDescription();
			if(_tokenItemVo != null)
				_itemDescription.acquisitionID = _tokenItemVo.id;
			else
				_itemDescription.acquisitionID = _itemVo.id;

			_stepController.gotoNextStep();
		}

		private function loadDropInfo():void
		{
			var rCriteria:Criteria = _em.createCriteria(RBossItemVO);
			rCriteria.addEqualsCondition("tbl_item_id", _itemDescription.acquisitionID);
			var rList:ArrayCollection = _em.fetchCriteria(rCriteria);
			for each(var r:RBossItemVO in rList)
			{
				var bossCriteria:Criteria = _em.createCriteria(TblBossVO);
				bossCriteria.addEqualsCondition("id", r.tbl_boss_id);
				var boss:TblBossVO = _em.fetchCriteriaFirstResult(bossCriteria) as TblBossVO;

				_itemDescription.addBossAcquisition(boss.zone_name, boss.restrict_name, boss.boss_name, boss.down_type);
			}

			_stepController.gotoNextStep();
		}

		private function loadVendorInfo():void
		{
			var rCriteria:Criteria = _em.createCriteria(RVendorSellVO);
			rCriteria.addEqualsCondition("tbl_item_id", _itemDescription.acquisitionID);
			var rList:ArrayCollection = _em.fetchCriteria(rCriteria);
			for each(var r:RVendorSellVO in rList)
			{
				var vendorCriteria:Criteria = _em.createCriteria(TblVendorVO);
				vendorCriteria.addEqualsCondition("id", r.tbl_vendor_id);
				var vendor:TblVendorVO = _em.fetchCriteriaFirstResult(vendorCriteria) as TblVendorVO;

				_itemDescription.addVendorAcquisition(vendor.vendor_type, r.cost);
			}

			_stepController.gotoNextStep();
		}

		private function loadItemProperty():void
		{
			var criteria:Criteria = _em.createCriteria(TblItemPropertyVO);
			criteria.addEqualsCondition("info_item_id", _itemVo.item_id);
			_itemPropertyVo = _em.fetchCriteriaFirstResult(criteria) as TblItemPropertyVO;
		}
	}
}